package com.nurt.service.impl;

import com.nurt.dao.impl.UserDaoImpl;
import com.nurt.entity.UserEntity;
import com.nurt.service.UserService;

import java.sql.SQLException;

public class UserServiceImpl implements UserService {
    private UserDaoImpl userDao;

    public UserServiceImpl() {
        this.userDao = new UserDaoImpl();
    }

    public UserEntity getUserInfo(UserEntity user) throws SQLException {
        return userDao.selectUser(user);
    }

    public UserEntity login(UserEntity user) throws SQLException {
        UserEntity userFromDb = userDao.selectUser(user);
        if (userFromDb != null && userFromDb.getPassword().equals(user.getPassword())) {
            return userFromDb;
        } else {
            return null;
        }
    }

    public int register(UserEntity newUser) throws SQLException {
        UserEntity user = userDao.selectUser(newUser);
        if (user != null) {
            return -1; // User already exists
        } else {
            return userDao.insertUser(newUser);
        }
    }

    public int updateUserInfo(UserEntity user) throws SQLException {
        return userDao.updateUser(user);
    }
}
